package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringFeedBackReplyForMnagerView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringFeedBackReplyForMnagerPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallDailyFeedbackConfirmPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/9.
 */

public class FeedBackReplyForMnagerActivity extends Activity implements View.OnClickListener,IEngineeringFeedBackReplyForMnagerView{
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView edt_feed_back_type, edt_exception_type, edt_unusual_reason, edt_advice, edt_impact, edt_replay_content, edt_confirm_person, edt_confirm_time;
    private TextView btn_confirm;
    //    private GridView gridView;
//    private GridAdapter gridAdapter;
//    private List<String> imgUrlList = new ArrayList<>();
    private EngineeringDailyFeedBackBean engineeringDailyFeedBackBean;
    private String orderId;
    private EngineeringFeedBackReplyForMnagerPresenter engineeringFeedBackReplyForMnagerPresenter;
    private String role = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_feedback_reply_for_super);

        engineeringFeedBackReplyForMnagerPresenter = new EngineeringFeedBackReplyForMnagerPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {

            engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra("feedbackbean");
            orderId = mIntent.getStringExtra(ConstantUtil.KEY_2);
            role = mIntent.getStringExtra("role");
//            orderParentBean = (ProduceOrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);

        edt_feed_back_type = findViewById(R.id.edt_feed_back_type);
        edt_exception_type = findViewById(R.id.edt_exception_type);
        edt_unusual_reason = findViewById(R.id.edt_unusual_reason);
        edt_advice = findViewById(R.id.edt_advice);
        edt_impact = findViewById(R.id.edt_impact);
        edt_replay_content = findViewById(R.id.edt_replay_content);
        edt_confirm_person = findViewById(R.id.edt_confirm_person);
        edt_confirm_time = findViewById(R.id.edt_confirm_time);
//
//        gridView = findViewById(R.id.gridView);
//        gridView.setNumColumns(3);
//        gridAdapter = new GridAdapter(this);
//        gridAdapter.setData(imgUrlList);
//        gridView.setAdapter(gridAdapter);

        btn_confirm = findViewById(R.id.btn_confirm);


        tv_title.setText("工程反馈");

        rl_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);


    }


    private void initData() {

        if (null != engineeringDailyFeedBackBean) {
            CommonEnum.FeedBackType feedBackType = engineeringDailyFeedBackBean.getType();
            if (null != feedBackType) {
                if (feedBackType == CommonEnum.FeedBackType.NORMAL) {
                    edt_feed_back_type.setText("正常反馈");
                } else if (feedBackType == CommonEnum.FeedBackType.UNUSUAL) {
                    edt_feed_back_type.setText("异常反馈");
                } else if (feedBackType == CommonEnum.FeedBackType.COMPLETED) {
                    edt_feed_back_type.setText("完工反馈");
                }
            }

            CommonEnum.SubType subType = engineeringDailyFeedBackBean.getSubType();
            if (null != subType) {
                if (subType == CommonEnum.SubType.DELAY) {
                    edt_exception_type.setText("物流延误");
                } else if (subType == CommonEnum.SubType.DAMAGE) {
                    edt_exception_type.setText("物流货损");
                } else if (subType == CommonEnum.SubType.INSTALL_UNUSUAL) {
                    edt_exception_type.setText("安装异常");
                } else if (subType == CommonEnum.SubType.OMIT) {
                    edt_exception_type.setText("物流漏货");
                }
            }

            edt_unusual_reason.setText(engineeringDailyFeedBackBean.getUnusualReason());
            edt_advice.setText(engineeringDailyFeedBackBean.getAdvice());
            edt_impact.setText(engineeringDailyFeedBackBean.getUnusualDays() + "");
            edt_replay_content.setText(engineeringDailyFeedBackBean.getCompleteUnusualDesc());
            edt_confirm_person.setText(engineeringDailyFeedBackBean.getCompleteUnusualAccountId());
            edt_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getCompleteUnusualDate()));

//
//            List<String> list = engineeringDailyFeedBackBean.getImgUrlList();
//            if (null != list) {
//                imgUrlList.addAll(list);
//                gridAdapter.setData(imgUrlList);
//                gridAdapter.notifyDataSetChanged();
//            }


            CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
            if (null != unusualStatus) {
                if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {


                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {

                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                    if (!TextUtils.isEmpty(role) && role.equals("super"))
                        btn_confirm.setVisibility(View.GONE);
                    else
                    btn_confirm.setVisibility(View.VISIBLE);

                } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                    btn_confirm.setVisibility(View.GONE);
                }
            }
        }

    }


    private void feedbackConfirm() {
        engineeringFeedBackReplyForMnagerPresenter.engineeringFeedBackRply();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

            case R.id.btn_confirm:
                feedbackConfirm();
                break;


        }
    }

    @Override
    public String getContent() {
        return null;
    }

    @Override
    public String getDailyFeedbackId() {
        if (null != engineeringDailyFeedBackBean)
            return engineeringDailyFeedBackBean.getId();
        return null;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }


    @Override
    public CommonEnum.EngineeringORderCategory getType() {

        return CommonEnum.EngineeringORderCategory.INSTALL;
    }

    @Override
    public void engineeringFeedBackRplySuccessed() {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void engineeringFeedBackRplyFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
